Ansible 安装与配置

1. ansible特点

        不需要安装客户端,通过sshd去通信

        基于模块工作,模块可以由任何语言开发

        不仅支持命令行使用模块,也支持编写yaml格式的playbook

        支持sudo

        有提供UI(浏览器图形化)www.ansible.com/tower 10台主机以内免费

        开源UI https://github.com/alaxli/ansible_ui

        文档 http://download.csdn.net/detail/liyang23456/7741185

2.ansible 安装

        两台机器 192.168.0.82 192.168.0.81

        只需要在192.168.0.82上安装ansible即可

1
2
[root@ansible ~]# yum install -y epel-release
[root@ansible ~]# yum install -y ansible

ansible 配置密钥

        192.168.0.82上生成密钥对

        ssh-keygen -t rsa 直接回车即可,不用设置密钥密码

1
[root@ansible ~]# ssh-keygen -t rsa

        把公钥(id_rsa.pub)内容放到对方机器(111)的/root/.ssh/authorized_keys里面

1
[root@ansible ~]# scp .ssh/id_rsa.pub 192.168.0.81:/root/.ssh/authorized_keys

01

        本机也要操作

1
[root@ansible ~]# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
1
[root@ansible ~]# chmod 600 /root/.ssh/authorized_keys

        关闭selinux

02

        检测连接

1
[root@ansible ~]# yum install -y openssh-clients
1
2
3
4
[root@ansible ~]# ssh 192.168.0.81
Nasty PTR record "192.168.0.81" is set up for 192.168.0.81, ignoring
Last login: Thu Feb 23 22:34:20 2017 from 192.168.0.100
[root@localhost ~]#

03

ansible 更改配置文件

1
[root@ansible ~]# vim /etc/ansible/hosts

        增加

1
2
3
[testhost]
127.0.0.1
192.168.0.81

04

        说明: testhost为主机组名字,自定义的。 下面两个ip为组内的机器ip。

3.Ansible 远程执行命令

1
[root@ansible ~]# ansible testhost -m command -a 'w'

05

        这样就可以批量执行命令了。这里的testhost 为主机组名,-m后边是模块名字,-a后面是命令。当然我们也可以直接写一个ip,针对某一台机器来执行命令。

1
[root@ansible ~]# ansible 127.0.0.1 -m command -a 'hostname'

06

        错误: “msg”: “Aborting, target uses selinux but python bindings (libselinux-python) aren’t installed!”

        解决:

1
[root@ansible ~]# yum install -y libselinux-python

        还有一个模块就是shell同样也可以实现

1
[root@ansible ~]# ansible testhost -m shell -a 'w'

        shell 和 command 的区别

07

4.ansible 拷贝文件或者目录

1
[root@ansible ~]# ansible testhost -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0644etest owner=root group=root mode=0755"

08

        注意:源目录会放到目标目录下面去,如果目标指定的目录不存在,它会自动创建。如果拷贝的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名。但相反,如果desc是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面。

1
[root@ansible ~]# ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/123"

        这里的/tmp/123和源机器上的/etc/passwd是一致的,但如果目标机器上已经有/tmp/123目录,则会再/tmp/123目录下面建立passwd文件.如果没有123目录,则直接创建123文件。

09

10

5.ansible 远程执行脚本

        首先创建一个shell脚本

1
[root@ansible ~]# vim /tmp/test.sh

        加入内容

1
2
3
#!/bin/bash
d=`date`
echo $d > /tmp/an_test.txt

11

        然后把该脚本分发到各个机器上

1
[root@ansible ~]# ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"

        最后是批量执行该shell脚本

1
[root@ansible ~]# ansible testhost -m shell -a "/tmp/test.sh"

12

13

        shell模块,还支持远程执行命令并且带管道

1
[root@ansible ~]# ansible testhost -m shell -a "cat /etc/passwd | wc -l"

14

6.ansible 实现任务计划

1
[root@ansible ~]# ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt' weekday=6"

15

        若要删除该cron 只需要加一个字段 state=absent

1
[root@ansible ~]# ansible testhost -m cron -a "name='test cron' state=absent"

16

        其他的时间表示:分钟 minute 小时 hour 日期 day 月份 month

7.Ansible安装rpm包管理服务

1
[root@ansible ~]# ansible testhost -m yum -a "name=httpd"

17

        在name后面还可以加上state=installed

1
[root@ansible ~]# ansible testhost -m service -a "name=httpd state=started enabled=yes"

18

19

        这里的name是centos系统里的服务名,可以通过chkconfig –list查到。

        Ansible文档的使用

        ansible-doc -l 列出所有的模块

20

        ansible-doc cron 查看指定模块的文档

21

8.ansible playbook 的使用

        相当于把模块写入到配置文件里面,例:

1
[root@ansible ~]# vim /etc/ansible/test.yml

        添加内容

1
2
3
4
5
6
---
- hosts: testhost
remote_user: root
tasks:
- name: test_playbook
shell: touch /tmp/yanyi.txt

22

        说明: hosts参数指定了对哪些主机进行参作;

        user参数指定了使用什么用户登录远程主机操作;

        tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来。

        执行:

1
[root@ansible ~]# ansible-playbook /etc/ansible/test.yml

23

        192.168.0.82

24

        192.168.0.81

25

        再来一个创建用户的例子:

1
[root@ansible ~]# vim /etc/ansible/create_user.yml

        添加内容

1
2
3
4
5
6
7
8
9
10
---
- name: create_user
hosts: testhost
user: root
gather_facts: false
vars:
- user: "test"
tasks:
- name: create user
user: name="{{ user }}"

26

        说明: name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略;gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;vars参数,指定了变量,这里指字一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。

1
[root@ansible ~]# ansible-playbook /etc/ansible/create_user.yml

27

        192.168.0.82

28

        192.168.0.81

29

9.ansible playbook 中的循环

1
[root@ansible ~]# vim /etc/ansible/loop.yml

        添加内容

1
2
3
4
5
6
7
8
9
10
---
- hosts: testhost
user: root
tasks:
- name: change mode for files
file: path=/tmp/{{ item }} mode=600
with_items:
- 1.txt
- 2.txt
- 3.txt

30

1
[root@ansible ~]# ansible-playbook /etc/ansible/loop.yml

31

        192.168.0.82 文件权限改为600

32

        192.168.0.81 文件权限改为600

33

10.ansible playbook 条件判断

1
[root@ansible ~]# vim /etc/ansible/when.yml

        添加内容

1
2
3
4
5
6
7
8
---
- hosts: testhost
user: root
gather_facts: True
tasks:
- name: use when
shell: touch /tmp/when.txt
when: ansible_eth0.ipv4.address == "192.168.0.82"

34

1
[root@ansible ~]# ansible-playbook /etc/ansible/when.yml

35

        ansible判断 出现FATAL all hosts have already failed – aborting 错误

11.ansible playbook 中的 handlers

        执行task之后,服务器发生变化之后要执行的一些操作,比如我们修改了配置文件后,需要重启一下服务

1
[root@ansible ~]# vim /etc/ansible/handlers.yml

        添加内容

1
2
3
4
5
6
7
8
9
10
11
---
- name: handlers test
hosts: 192.168.0.81
user: root
tasks:
- name: copy file
copy: src=/etc/passwd dest=/tmp/aaa.txt
notify: test handlers
handlers:
  - name: test handlers
shell: echo "111111" >> /tmp/aaa.txt

36

        说明,只有copy模块真正执行后,才会去调用下面的handlers相关的操作。也就是说如果1.txt和2.txt内容是一样的,并不会去执行handlers里面的shell相关命令。 这种比较适合配置文件发生更改后,重启服务的操作。

1
[root@ansible ~]# ansible-playbook /etc/ansible/handlers.yml

        客户端查看

37

12.ansible 安装 nginx

        思路:先在一台机器上编译安装好nginx、打包,然后再用ansible去下发

        进入ansible配置文件目录

1
[root@ansible ~]# cd /etc/ansible

        创建一个nginx_install的目录,方便管理

1
[root@ansible ansible]# mkdir nginx_install
1
[root@ansible ansible]# cd nginx_install/
1
[root@ansible nginx_install]# mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}

        说明:roles目录下有两个角色,common为一些准备操作,install为安装nginx的操作。每个角色下面又有几个目录,handlers下面是当发生改变时要执行的操作,通常用在配置文件发生改变,重启服务。files为安装时用到的一些文件,meta为说明信息,说明角色依赖等信息,tasks里面是核心的配置文件,templates通常存一些配置文件,启动脚本等模板文件,vars下为定义的变量

        需要事先准备好安装用到的文件,具体如下:

        在一台机器上事先编译安装好nginx,配置好启动脚本,配置好配置文件

        安装好后,我们需要把nginx目录打包

1
2
[root@ansible ~]# cd /usr/local
[root@ansible local]# tar zcvf nginx.tar.gz nginx

        并放到/etc/ansible/nginx_install/roles/install/files/下面,名字为nginx.tar.gz

1
[root@ansible nginx_install]# mv /usr/local/nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/

        启动脚本、配置文件都要放/etc/ansible/nginx_install/roles/install/templates下面

1
2
3
[root@ansible roles]# cd /etc/ansible/nginx_install/roles/install
[root@ansible install]# cp /usr/local/nginx/conf/nginx.conf templates/
[root@ansible install]# cp /etc/init.d/nginx templates/

        定义common的tasks,nginx是需要一些依赖包的

1
[root@ansible nginx_install]# cd /etc/ansible/nginx_install/roles
1
[root@ansible roles]# vim ./common/tasks/main.yml

        内容如下

1
2
3
4
5
6
7
---
- name: Install initializtion require software
yum: name={{ item }} state=installed
with_items:
- zlib-devel
- pcre-devel
- openssl-devel

38

        定义变量

1
[root@ansible install]# vim /etc/ansible/nginx_install/roles/install/vars/main.yml

        内容如下

1
2
3
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx

39

        首先要把所有用到的文档拷贝到目标机器

1
[root@ansible install]# vim /etc/ansible/nginx_install/roles/install/tasks/copy.yml

        内容如下

1
2
3
4
5
6
7
8
- name: Copy Nginx Software
copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root
- name: Uncompression Nginx Software
shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/
- name: Copy Nginx Start Script
template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755
- name: Copy Nginx Config
template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644

40

        接下来会建立用户,启动服务,删除压缩包

1
[root@ansible ~]# vim /etc/ansible/nginx_install/roles/install/tasks/install.yml

        内容如下

1
2
3
4
5
6
7
8
- name: Create Nginx User
user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin
- name: Start Nginx Service
service: name=nginx state=restarted
- name: Add Boot Start Nginx Service
shell: chkconfig --level 345 nginx on
- name: Delete Nginx compression files
shell: rm -rf /tmp/nginx.tar.gz

41

        再创建main.yml并且把copy和install调用

1
[root@ansible ~]# vim /etc/ansible/nginx_install/roles/install/tasks/main.yml

        内容如下

1
2
- include: copy.yml
- include: install.yml

42

        到此两个roles:common和install就定义完成了,接下来要定义一个入口配置文件

1
[root@ansible ~]# vim /etc/ansible/nginx_install/install.yml

        内容如下

1
2
3
4
5
6
7
---
- hosts: testhost
remote_user: root
gather_facts: True
roles:
- common
- install

43

        执行: ansible-playbook /etc/ansible/nginx_install/install.yml

1
[root@ansible ~]# ansible-playbook /etc/ansible/nginx_install/install.yml

44

45

        注意:执行之前一定要检查所有机器 80 端口是否占用,否则会出错!

        客户端查看

46

47

        下载整个样例库

        git clone git://github.com/dl528888/ansible-examples.git

1
[root@ansible ~]# git clone git://github.com/dl528888/ansible-examples.git

        git命令,需要yum先安装一下:

1
[root@ansible ~]# yum install -y git

13.ansible 管理配置文件

        生产环境中大多时候是需要管理配置文件的,安装软件包只是在初始化环境的时候用一下。下面我们来写个管理nginx配置文件的playbook

1
[root@ansible ~]# mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}

        其中new为更新时用到的,old为回滚时用到的,files下面为nginx.conf和vhosts目录,handlers为重启nginx服务的命令
关于回滚,需要在执行playbook之前先备份一下旧的配置,所以对于老配置文件的管理一定要严格,千万不能随便去修改线上机器的配置,并且要保证new/files下面的配置和线上的配置一致

        先把nginx.conf和vhosts目录放到files目录下面

1
[root@ansible ~]# cp /usr/local/nginx/conf/nginx.conf /etc/ansible/nginx_config/roles/new/files/
1
[root@ansible ~]# cp -r /usr/local/nginx/conf/vhosts /etc/ansible/nginx_config/roles/new/files/
1
[root@ansible ~]# vim /etc/ansible/nginx_config/roles/new/vars/main.yml

        定义变量

1
nginx_basedir: /usr/local/nginx

48

1
[root@ansible ~]# vim /etc/ansible/nginx_config/roles/new/handlers/main.yml

        定义重新加载nginx服务

1
2
- name: restart nginx
shell: /etc/init.d/nginx reload

49

1
[root@ansible ~]# vim /etc/ansible/nginx_config/roles/new/tasks/main.yml

        这是核心的任务

1
2
3
4
5
6
- name: copy conf file
copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.dest }} backup=yes owner=root group=root mode=0644
with_items:
- { src: nginx.conf, dest: conf/nginx.conf }
- { src: vhosts, dest: conf/ }
notify: restart nginx

50

1
[root@ansible ~]# vim /etc/ansible/nginx_config//update.yml

        最后是定义总入口配置

1
2
3
4
5
---
- hosts: testhost
user: root
roles:
- new

51

        执行:

1
[root@ansible ~]# ansible-playbook /etc/ansible/nginx_config/update.yml

52

        而回滚的backup.yml对应的roles为old

1
[root@ansible ~]# rsync -av /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/
1
[root@ansible ~]# ansible-playbook /etc/ansible/nginx_config/backup.yml

53

        回滚操作就是把旧的配置覆盖,然后重新加载nginx服务